home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Dialectic 1.2 / source / Dialectic ƒ / Shell ƒ / integrity.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  4.6 KB  |  135 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        integrity.c
  4.  
  5. Purpose:    This module implements a quick-and-dirty integrity check:
  6.             compare the resource fork and map length to stored values.
  7.  
  8. \**********************************************************************/
  9.  
  10. #include "integrity.h"
  11.  
  12. #define RESOURCE_FORK_DATA_LENGTH_OFFSET        8L
  13. #define RESOURCE_FORK_MAP_LENGTH_OFFSET            12L
  14. #define STORED_DATA_LENGTH_OFFSET                128L
  15. #define STORED_MAP_LENGTH_OFFSET                132L
  16. #define TAG_OFFSET                                136L
  17. #define THE_TAG                                    0x16435934
  18.  
  19. Boolean DoIntegrityCheck(Boolean *programIntegritySet)
  20. /* called first thing; can be used either to check program integrity (if it has
  21.    been installed) or to install the integrity checker (if the SHIFT key is
  22.    help down during program launch).  Returns FALSE if program integrity is
  23.    not verified; programIntegritySet is TRUE if this procedure installs the
  24.    integrity checker. */
  25. {
  26.     int                thisFile;
  27.     long            count;
  28.     long            resDataLength, checkData;
  29.     long            resMapLength, checkMap;
  30.     long            resMapOffset;
  31.     int                resAttributes;
  32.     long            tag;
  33.     unsigned int    theKeys[8];
  34.     
  35.     *programIntegritySet=FALSE;
  36.     GetKeys(&theKeys);        /* get raw key map */
  37.     FlushVol(0L, 0);        /* just to be on the safe side */
  38.     
  39.     /* interestingly enough, the new-and-improved functions to open a resource fork
  40.        (HOpenRF & FSpOpenRF) won't work here.  They check permissions and won't
  41.        allow write permission to an already-open resource fork -- so we won't be
  42.        able to install the integrity checker on the fly if we Do The Right Thing™.
  43.        Luckily, the old-and-inferior function (OpenRF) doesn't ask permission... */
  44.     OpenRF(CurApName, 0, &thisFile);
  45.     
  46.     SetFPos(thisFile, 1, RESOURCE_FORK_DATA_LENGTH_OFFSET);
  47.     count=4L;
  48.     /* get length of resource data */
  49.     if (FSRead(thisFile, &count, (Ptr)(&resDataLength))!=noErr)
  50.         return FALSE;
  51.     
  52.     SetFPos(thisFile, 1, RESOURCE_FORK_MAP_LENGTH_OFFSET);
  53.     count=4L;
  54.     /* get length of resource map */
  55.     if (FSRead(thisFile, &count, (Ptr)(&resMapLength))!=noErr)
  56.         return FALSE;
  57.     
  58.     SetFPos(thisFile, 1, TAG_OFFSET);
  59.     count=4L;
  60.     FSRead(thisFile, &count, (Ptr)(&tag));    /* read long-word tag */
  61.     if (tag!=THE_TAG)    /* if not equal to our tag, integrity checker not installed */
  62.     {
  63.         if (theKeys[3]&1)    /* if SHIFT key down, install integrity checker */
  64.         {
  65.             count=4L;
  66.             SetFPos(thisFile, 1, 4L);
  67.             /* get the offset of the start of the resource map in the resource fork */
  68.             if (FSRead(thisFile, &count, (Ptr)(&resMapOffset))!=noErr)
  69.                 return FALSE;
  70.             
  71.             resMapOffset+=22L;    /* resource fork attributes at map+22 */
  72.             count=2L;
  73.             SetFPos(thisFile, 1, resMapOffset);
  74.             /* get old resource fork attributes */
  75.             if (FSRead(thisFile, &count, (Ptr)(&resAttributes))!=noErr)
  76.                 return FALSE;
  77.             
  78.             resAttributes|=0x8000;    /* lock resource fork */
  79.             count=2L;
  80.             SetFPos(thisFile, 1, resMapOffset);
  81.             /* rewrite new resource fork attributes */
  82.             /* It is a little-known fact that this trick makes the application
  83.                immune to all known viruses -- no known virus bothers to unlock the
  84.                resource fork before attempting to infect, and ABSOLUTELY NO changes
  85.                can be made to the resource fork when it's locked like this.  On the
  86.                other hand, you better be damn sure that the application has no
  87.                viruses in it when you install the integrity checker -- if it's
  88.                infected when this code is run, you won't be able to disinfect it
  89.                until you unlock the resource fork. */
  90.             if (FSWrite(thisFile, &count, (Ptr)(&resAttributes))!=noErr)
  91.                 return FALSE;
  92.             
  93.             SetFPos(thisFile, 1, STORED_DATA_LENGTH_OFFSET);
  94.             count=4L;
  95.             /* store length of resource data */
  96.             if (FSWrite(thisFile, &count, (Ptr)(&resDataLength))!=noErr)
  97.                 return FALSE;
  98.             
  99.             SetFPos(thisFile, 1, STORED_MAP_LENGTH_OFFSET);
  100.             count=4L;
  101.             /* store length of resource map */
  102.             if (FSWrite(thisFile, &count, (Ptr)(&resMapLength))!=noErr)
  103.                 return FALSE;
  104.             
  105.             SetFPos(thisFile, 1, TAG_OFFSET);
  106.             count=4L;
  107.             tag=THE_TAG;
  108.             /* store tag so we know integrity checker is installed */
  109.             if (FSWrite(thisFile, &count, (Ptr)(&tag))!=noErr)
  110.                 return FALSE;
  111.             
  112.             *programIntegritySet=TRUE;        /* so we know we've installed it */
  113.         }
  114.         
  115.         return TRUE;
  116.     }
  117.     else
  118.     {
  119.         SetFPos(thisFile, 1, STORED_DATA_LENGTH_OFFSET);
  120.         count=4L;
  121.         /* get stored resource data length */
  122.         if (FSRead(thisFile, &count, (Ptr)(&checkData))!=noErr)
  123.             return FALSE;
  124.         
  125.         SetFPos(thisFile, 1, STORED_MAP_LENGTH_OFFSET);
  126.         count=4L;
  127.         /* get stored resource map length */
  128.         if (FSRead(thisFile, &count, (Ptr)(&checkMap))!=noErr)
  129.             return FALSE;
  130.         
  131.         /* check real resource data/map lengths to stored values */
  132.         return ((resDataLength==checkData) && (resMapLength==checkMap));
  133.     }
  134. }
  135.